<template>
  <div class="container">
    <el-tabs v-model="message">
      <el-tab-pane :label="`未读消息(${state.unread.length})`" name="first">
        <el-table :data="state.unread" :show-header="false" style="width: 100%">
          <el-table-column>
            <template #default="scope">
              <span class="message-title">{{ scope.row.content }}</span>
            </template>
          </el-table-column>
          <el-table-column width="180">
            <template #default="scope">
              <span>{{ moment(scope.row.updateTime).format('YYYY-MM-DD,hh:mm:ss') }}</span>
            </template>
          </el-table-column>
          <el-table-column width="120">
            <template #default="scope">
              <el-button size="small" @click="handleRead(scope.$index)">标为已读</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="handle-row">
          <el-button type="primary">全部标为已读</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="`已读消息(${state.read.length})`" name="second">
        <template v-if="message === 'second'">
          <el-table :data="state.read" :show-header="false" style="width: 100%">
            <el-table-column>
              <template #default="scope">
                <span class="message-title">{{ scope.row.content }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="updateTime" width="180"></el-table-column>
            <el-table-column width="120" v-if="state.read">
              <template #default="scope">
                <el-button type="danger" size="small" @click="handleDel(scope.$index)"
                  >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="handle-row">
            <el-button type="danger">删除全部</el-button>
          </div>
        </template>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="tabs">
import { ref, onMounted } from 'vue'
import { MassageControllerService } from '../../../generated'
import moment from 'moment'

const message = ref('first')
const state = ref({
  unread: [
    {
      date: '2018-04-19 20:00:00',
      title: '【系统通知】该系统将于今晚凌晨2点到5点进行升级维护'
    },
    {
      date: '2018-04-19 21:00:00',
      title: '今晚12点整发大红包，先到先得'
    }
  ],
  read: [
    // {
    //   date: '2018-04-19 20:00:00',
    //   title: '【系统通知】该系统将于今晚凌晨2点到5点进行升级维护'
    // }
  ]
})
/**
 * 加载未读信息
 */
const getUnreadMsg = async () => {
  const res = await MassageControllerService.getUnReadUsingGet()
  console.log(res.data)

  state.value.unread = res.data // [{ date: '2018-04-19 20:00:00', title: '666' }]
}
/**
 * 加载已读信息
 */
const getReadMsg = () => {
  // state.value.read = [{ date: '2018-04-19 20:00:00', title: '666' }]
}

onMounted(() => {
  getUnreadMsg()
  getReadMsg()
  // console.log(state.value.unread)
})

/**
 * 标记已读
 * @param index
 */
const handleRead = (index: number) => {
  const item = state.value.unread.splice(index, 1)
  state.value.read = item.concat(state.value.read)
}
/**
 * 删除信息
 * @param index
 */
const handleDel = (index: number) => {
  const item = state.value.read.splice(index, 1)
  state.value.recycle = item.concat(state.value.recycle)
}
/**
 * 还原
 * @param index
 */
const handleRestore = (index: number) => {
  const item = state.value.recycle.splice(index, 1)
  state.value.read = item.concat(state.value.read)
}
</script>

<style>
.message-title {
  cursor: pointer;
}

.handle-row {
  margin-top: 30px;
}
</style>
